<% layout('/layouts/default.html', {title: '创建报表', libs: ['validate']}){ %>
<style>
    #reportTable{
        width: 90%;
        max-width: 500px;
        margin-top: 10px;
    }
</style>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa fa-list-alt"></i> ${text(record.isNewRecord ? '报表' : '编辑record')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
        <table id="reportTable" border="1" align="center" width="100%">
            <tr>
                <th colspan="2" align="center">
                    <div class="form-inline" align="center">
                        <#form:input id="startTime" path="startTime" readonly="readonly" maxlength="20" class="form-control Wdate-date"
                        dataFormat="date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false,onpicked:function(){createDate_lte.click()}});"/>
                        &nbsp;至&nbsp;
                        <#form:input id="endTime" path="endTime" readonly="readonly" maxlength="20" class="form-control Wdate-date"
                        dataFormat="date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
                    </div>
                </th>
            </tr>
            <tr>
                <td align="center" id="one" onclick="choose(1)" style="height: 30px">今天</td>
                <td align="center" id="two" onclick="choose(2)" style="height: 30px">昨天</td>
            </tr>
            <tr>
                <td align="center" id="three" onclick="choose(3)" style="height: 30px">本星期</td>
                <td align="center" id="four" onclick="choose(4)" style="height: 30px">上星期</td>
            </tr>
            <tr>
                <td align="center" id="five" onclick="choose(5)" style="height: 30px">本月</td>
                <td align="center" id="six" onclick="choose(6)" style="height: 30px">上月</td>
            </tr>
        </table>
        <div style="text-align: center;padding-top: 30px"><button style="width: 100px" class="btn btn-success" onclick="search()">查询</button></div>
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>



	</div>
</div>
<% } %>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
function choose(num) {
    var one = document.getElementById("one");
    var two = document.getElementById("two");
    var three = document.getElementById("three");
    var four = document.getElementById("four");
    var five = document.getElementById("five");
    var six = document.getElementById("six");
    var now = new Date();
    var date = getDate(now);//获取系统当前时间
    var yesterday = getDate(now.getTime()-1*24*3600*1000);//获取昨天时间
    if (num==1){
        clearColor();
        one.style.backgroundColor="#FFA500";
        $("#startTime").val(date);
        $("#endTime").val(date);
    }else if(num==2){
        clearColor();
        two.style.backgroundColor="#FFA500";
        $("#startTime").val(yesterday);
        $("#endTime").val(yesterday);
    }else if(num==3){
        clearColor();
        three.style.backgroundColor="#FFA500";
        $("#startTime").val(getWeekStartDate());
        $("#endTime").val(date);
    }else if(num==4){
        clearColor();
        four.style.backgroundColor="#FFA500";
        $("#startTime").val(getLastWeekStartDate());
        $("#endTime").val(getLastWeekEndDate());
    }else if(num==5){
        clearColor();
        five.style.backgroundColor="#FFA500";
        $("#startTime").val(getMonthFirstday());
        $("#endTime").val(date);
    }else if(num==6){
        clearColor();
        six.style.backgroundColor="#FFA500";
        $("#startTime").val(getLastMonthStartDate());
        $("#endTime").val(getLastMonthEndDate());
    }
}
function clearColor() {
    document.getElementById("one").style.backgroundColor="#FFFFFF";
    document.getElementById("two").style.backgroundColor="#FFFFFF";
    document.getElementById("three").style.backgroundColor="#FFFFFF";
    document.getElementById("four").style.backgroundColor="#FFFFFF";
    document.getElementById("five").style.backgroundColor="#FFFFFF";
    document.getElementById("six").style.backgroundColor="#FFFFFF";
}

function getDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year, month, day].join('-');
}


//时间
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
//格式化日期：yyyy-MM-dd
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();
    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}

//获得本周的开始日期
function getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    return formatDate(weekStartDate);
}



//获得本周的结束日期
function getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    return formatDate(weekEndDate);
}


//获得上周的开始日期
function getLastWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
    return formatDate(weekStartDate);
}


//获得上周的结束日期
function getLastWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
    return formatDate(weekEndDate);
}

//获取本月第一天
function getMonthFirstday() {
    var d = new Date();
    month = '' + (d.getMonth() + 1),
    year = d.getFullYear();
    if (month < 10) {
        month = "0" + month;
    }
    return year+'-'+month+"-01"
}

//获得上月开始时间
function getLastMonthStartDate(){
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
}
//获得上月结束时间
function getLastMonthEndDate(){
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
}
//获得某月的天数　　
function getMonthDays(myMonth){
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
    return days;
}

function search() {
    if($("#startTime").val()==""||$("#endTime").val()==""){
        alert("请先选择时间！");
        return;
    }
    window.location.href="${ctx}/vip/record/agentRecordList?startTime="+$("#startTime").val()+"&endTime="+$("#endTime").val();
}
</script>